const express = require('express')
const syncService = require('../services/syncService')
const auth = require('../middleware/auth')
const logger = require('../utils/logger')

const router = express.Router()

/**
 * 同步用户数据
 * POST /api/sync/user-data
 */
router.post('/user-data', auth, async (req, res) => {
  try {
    const result = await syncService.syncUserData(req.user.id)

    res.json({
      success: true,
      code: 1000,
      message: '数据同步成功',
      data: result
    })

  } catch (error) {
    logger.error('同步用户数据接口错误:', error)
    
    res.status(400).json({
      success: false,
      code: 1005,
      message: error.message || '数据同步失败'
    })
  }
})

/**
 * 检查同步状态
 * GET /api/sync/status
 */
router.get('/status', auth, async (req, res) => {
  try {
    const result = await syncService.checkSyncStatus(req.user.id)

    res.json({
      success: true,
      code: 1000,
      message: '获取同步状态成功',
      data: result
    })

  } catch (error) {
    logger.error('检查同步状态接口错误:', error)
    
    res.status(400).json({
      success: false,
      code: 1005,
      message: error.message || '获取同步状态失败'
    })
  }
})

/**
 * 强制同步数据
 * POST /api/sync/force
 */
router.post('/force', auth, async (req, res) => {
  try {
    const result = await syncService.forceSyncUserData(req.user.id)

    res.json({
      success: true,
      code: 1000,
      message: '强制同步成功',
      data: result
    })

  } catch (error) {
    logger.error('强制同步接口错误:', error)
    
    res.status(400).json({
      success: false,
      code: 1005,
      message: error.message || '强制同步失败'
    })
  }
})

/**
 * 获取跨端登录记录
 * GET /api/sync/cross-platform-logins
 */
router.get('/cross-platform-logins', auth, async (req, res) => {
  try {
    const result = await syncService.getUserCrossPlatformLogins(req.user.id)

    res.json({
      success: true,
      code: 1000,
      message: '获取跨端登录记录成功',
      data: result
    })

  } catch (error) {
    logger.error('获取跨端登录记录接口错误:', error)
    
    res.status(400).json({
      success: false,
      code: 1005,
      message: error.message || '获取跨端登录记录失败'
    })
  }
})

/**
 * 检查账户绑定状态
 * GET /api/sync/binding-status
 */
router.get('/binding-status', auth, async (req, res) => {
  try {
    const result = await syncService.checkAccountBindingStatus(req.user.id)

    res.json({
      success: true,
      code: 1000,
      message: '获取绑定状态成功',
      data: result
    })

  } catch (error) {
    logger.error('检查账户绑定状态接口错误:', error)
    
    res.status(400).json({
      success: false,
      code: 1005,
      message: error.message || '获取绑定状态失败'
    })
  }
})

/**
 * 获取跨端状态
 * GET /api/sync/cross-platform-state
 */
router.get('/cross-platform-state', auth, async (req, res) => {
  try {
    const result = await syncService.getCrossPlatformState(req.user.id)

    res.json({
      success: true,
      code: 1000,
      message: '获取跨端状态成功',
      data: result
    })

  } catch (error) {
    logger.error('获取跨端状态接口错误:', error)
    
    res.status(400).json({
      success: false,
      code: 1005,
      message: error.message || '获取跨端状态失败'
    })
  }
})

/**
 * 同步跨端用户状态
 * POST /api/sync/cross-platform-state
 */
router.post('/cross-platform-state', auth, async (req, res) => {
  try {
    const result = await syncService.syncCrossPlatformUserState(req.user.id)

    res.json({
      success: true,
      code: 1000,
      message: '跨端状态同步成功',
      data: result
    })

  } catch (error) {
    logger.error('同步跨端状态接口错误:', error)
    
    res.status(400).json({
      success: false,
      code: 1005,
      message: error.message || '跨端状态同步失败'
    })
  }
})

/**
 * 验证数据一致性
 * POST /api/sync/validate-consistency
 */
router.post('/validate-consistency', auth, async (req, res) => {
  try {
    const result = await syncService.validateDataConsistency(req.user.id)

    res.json({
      success: true,
      code: 1000,
      message: '数据一致性验证完成',
      data: result
    })

  } catch (error) {
    logger.error('验证数据一致性接口错误:', error)
    
    res.status(400).json({
      success: false,
      code: 1005,
      message: error.message || '数据一致性验证失败'
    })
  }
})

module.exports = router